package com.chenyuxin.ilp.controller.file;

import com.chenyuxin.ilp.model.dto.file.AddFileMistakeDto;
import com.chenyuxin.ilp.model.dto.file.AddFolderMistakeDto;
import com.chenyuxin.ilp.model.pojo.Result;
import com.chenyuxin.ilp.service.file.FileMistakeService;
import com.chenyuxin.ilp.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping("/file/mistake")
public class FileMistakeController {
    @Autowired
    private FileMistakeService fileMistakeService;
    @PostMapping("/folder")
    public Result addMistakeFolder(@RequestBody AddFolderMistakeDto addFolderMistakeDto){
        String userId = SecurityUtil.getUserId();
        return fileMistakeService.addMistakeFolder(addFolderMistakeDto,userId);
    }
    @PostMapping("/file")
    public Result addMistakeFile(@RequestParam("folderid") Long folderId,
                                 @RequestPart MultipartFile file){
        String userId = SecurityUtil.getUserId();
        return fileMistakeService.addMistakeFile(folderId,file,userId);
    }
    // 删除错题文件夹
    @DeleteMapping("/folder/{folderid}")
    public Result deleteMistakeFolder(@PathVariable("folderid")Long folderId){
        String userId = SecurityUtil.getUserId();
        return fileMistakeService.deleteMistakeFolder(folderId,userId);
    }
    // 删除错题文件
    @DeleteMapping("/file/{fileid}")
    public Result deleteMistakeFile(@PathVariable("fileid")Long fileId){
        String userId = SecurityUtil.getUserId();
        return fileMistakeService.deleteMistakeFile(fileId,userId);
    }
    // 错题预览
    @GetMapping("/preview/{fileid}")
    public Result previewMistakeFile(@PathVariable("fileid")Long fileId){
        String userId = SecurityUtil.getUserId();
        return fileMistakeService.previewMistakeFile(fileId,userId);
    }
    // 下载错题文件
    @GetMapping("/download/{fileid}")
    public ResponseEntity<byte[]> downloadMistakeFile(@PathVariable("fileid") Long fileId){
        String userId = SecurityUtil.getUserId();
        return fileMistakeService.downloadMistakeFile(userId,fileId);
    }
    // 查询用户错题文件夹
    @GetMapping("/folder")
    public Result selectMistakeFolder(){
        String userId = SecurityUtil.getUserId();
        return fileMistakeService.selectMistakeFolder(userId);
    }
    // 查询用户错题文件
    @GetMapping("/{folderid}")
    public Result selectMistakeFile(@PathVariable("folderid") Long folderId){
        String userId = SecurityUtil.getUserId();
        return fileMistakeService.selectMistakeFile(folderId,userId);
    }
}
